package com.yuyou.fn.timer.task;


import com.github.wens.mybatisplus.annotations.TableId;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.datadeal.service.IStudentMergeService;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.entity.BaseSubject;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IBaseSubjectService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.platform.entity.SysDataPermission;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.service.ISysDataPermissionService;
import com.yuyou.fn.platform.service.ISysUserService;
import com.yuyou.fn.settlement.service.IPayRecordService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wens on 2019/03/14.
 */
@JobHander(value="OnceTask")
@Component
public class OnceTask extends IJobHandler {

    @Resource
    private ISysDataPermissionService sysDataPermissionService ;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private IBaseSubjectService baseSubjectService;

    @Resource
    private IBaseGradeService baseGradeService;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {
        XxlJobLogger.log(Arrays.toString(params) );
        List<SysUser> sysUsers = sysUserService.findSysUserByStatus(1);
        List<BaseGrade> baseGradeList=baseGradeService.findAllOrderGrade();
        List<BaseSubject> baseSubjectList=baseSubjectService.findAllFromCache();
        sysDataPermissionService.deleteByResourceTypes(Arrays.asList(3,4));
        sysDataPermissionService.clearCache();
        List<SysDataPermission> sysDataPermissionList=new ArrayList<>(sysUsers.size()*baseGradeList.size()+sysUsers.size()*baseSubjectList.size());
        for (SysUser sysUser:sysUsers){
            for (BaseGrade baseGrade:baseGradeList){
                SysDataPermission sysDataPermission=new SysDataPermission();
                sysDataPermission.setResourceId(baseGrade.getGradeId());
                sysDataPermission.setResourceType(3);
                sysDataPermission.setCreateTime(TimeUtils.now());
                sysDataPermission.setUpdateTime(TimeUtils.now());
                sysDataPermission.setUserId(sysUser.getUserId());
                sysDataPermissionList.add(sysDataPermission);
            }

            for (BaseSubject baseSubject:baseSubjectList){
                SysDataPermission sysDataPermission=new SysDataPermission();
                sysDataPermission.setResourceId(baseSubject.getSubjectId());
                sysDataPermission.setResourceType(4);
                sysDataPermission.setCreateTime(TimeUtils.now());
                sysDataPermission.setUpdateTime(TimeUtils.now());
                sysDataPermission.setUserId(sysUser.getUserId());
                sysDataPermissionList.add(sysDataPermission);
            }
        }
        sysDataPermissionService.insertBatch(sysDataPermissionList);


       // sysDataPermissionService.saveDataPermission();
        return ReturnT.SUCCESS;
    }
}
